package com.brand.prisd.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.brand.prisd.R;
import com.brand.prisd.bean.LampBean;
import com.brand.prisd.sp.MySharedPreferences;
import com.orhanobut.logger.Logger;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IDevListener;
import com.tuya.smart.sdk.api.IResultCallback;
import com.tuya.smart.sdk.api.ITuyaDevice;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ControlSwitchActivity extends AppCompatActivity implements IDevListener {


    @BindView(R.id.iv_left_arrow_white)
    ImageView ivLeftArrowWhite;
    @BindView(R.id.iv_edit)
    ImageView ivEdit;
    @BindView(R.id.cl_title)
    ConstraintLayout clTitle;
    @BindView(R.id.iv_switch1)
    ImageView ivSwitch1;
    @BindView(R.id.tv_switch1)
    TextView tvSwitch1;
    @BindView(R.id.iv_edit1)
    ImageView ivEdit1;
    @BindView(R.id.iv_switch2)
    ImageView ivSwitch2;
    @BindView(R.id.tv_switch2)
    TextView tvSwitch2;
    @BindView(R.id.iv_edit2)
    ImageView ivEdit2;
    @BindView(R.id.iv_switch3)
    ImageView ivSwitch3;
    @BindView(R.id.tv_switch3)
    TextView tvSwitch3;
    @BindView(R.id.iv_edit3)
    ImageView ivEdit3;
    @BindView(R.id.cl_switch)
    ConstraintLayout clSwitch;
    @BindView(R.id.iv_lights_all)
    ImageView ivLightsAll;
    @BindView(R.id.iv_timing)
    ImageView ivTiming;
    @BindView(R.id.iv_all_closed)
    ImageView ivAllClosed;
    @BindView(R.id.tv_lights_all)
    TextView tvLightsAll;
    @BindView(R.id.tv_timing)
    TextView tvTiming;
    @BindView(R.id.tv_all_close)
    TextView tvAllClose;
    @BindView(R.id.cl_bottom)
    ConstraintLayout clBottom;
    private LampBean lampBean;
    private ITuyaDevice mDevice;
    private static final String STHEME_LAMP_DPID_1 = "1"; //灯开关

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control_switch);
        ButterKnife.bind(this);
        lampBean = new LampBean();
        initMDevice();
    }

    private void initMDevice() {
        String devId = MySharedPreferences.Build(this).getString("deviceId", null);
        Logger.i("devId: " + devId);
        if (devId != null) {
            //根据设备id初始化设备控制类
            mDevice = TuyaHomeSdk.newDeviceInstance(devId);
            /**
             * 设备对象。该设备的所有dp变化都会通过callback返回。
             *
             * 初始化设备之前，请确保已经初始化连接服务端，否则无法获取到服务端返回信息
             */
            mDevice.registerDevListener(this);
        }
    }

    public void openLamp(final Context context, LampBean bean, boolean val) {
        bean.setOpen(val);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(STHEME_LAMP_DPID_1, bean.isOpen());
        if (mDevice == null) {
            Toast.makeText(context, "控制失败！", Toast.LENGTH_SHORT).show();
        } else {
            mDevice.publishDps(JSONObject.toJSONString(hashMap), new IResultCallback() {
                @Override
                public void onError(String s, String s1) {
                    Toast.makeText(context, "控制失败:" + s + ";" + s1, Toast.LENGTH_SHORT).show();
                    Logger.e(s + " " + s1);
                }

                @Override
                public void onSuccess() {
//                Toast.makeText(context, "开灯成功", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDevice != null) {
            mDevice.unRegisterDevListener();
            mDevice.onDestroy();
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @OnClick({R.id.iv_left_arrow_white, R.id.iv_edit, R.id.iv_switch1, R.id.iv_edit1, R.id.iv_switch2, R.id.iv_edit2, R.id.iv_switch3, R.id.iv_edit3, R.id.iv_lights_all, R.id.iv_timing, R.id.iv_all_closed, R.id.tv_lights_all, R.id.tv_timing, R.id.tv_all_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_arrow_white:
                finish();
                break;
            case R.id.iv_edit:
            case R.id.iv_edit1:
            case R.id.iv_edit2:
            case R.id.iv_edit3:
            case R.id.iv_timing:
            case R.id.tv_timing:
                Toast.makeText(this, "待开发...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_switch2:
                if (ivSwitch2.isSelected()) {
                    ivSwitch2.setSelected(false);
                } else {
                    ivSwitch2.setSelected(true);
                }
                break;
            case R.id.iv_switch3:
                if (!ivSwitch3.isSelected()) {
                    ivSwitch3.setSelected(true);
                } else {
                    ivSwitch3.setSelected(false);
                }
                break;
            case R.id.iv_switch1:
                if (!lampBean.isOpen()) {
                    ivSwitch1.setSelected(true);
                    openLamp(this, lampBean, true);
                } else {
                    ivSwitch1.setSelected(false);
                    openLamp(this, lampBean, false);
                }
                break;
            case R.id.iv_lights_all:
            case R.id.tv_lights_all:
                setSelected(true);
                openLamp(this, lampBean, true);
                break;
            case R.id.iv_all_closed:
            case R.id.tv_all_close:
                setSelected(false);
                openLamp(this, lampBean, false);
                break;
        }
    }

    private void setSelected(boolean b) {
        ivSwitch1.setSelected(b);
        ivSwitch2.setSelected(b);
        ivSwitch3.setSelected(b);
    }

    @Override
    public void onDpUpdate(String devId, String dpStr) {
        //dp数据更新:devId 和相应dp数据
        Logger.i("dp数据更新\n" + "devId: " + devId + "   " + "dpStr: " + dpStr);
    }

    @Override
    public void onRemoved(String devId) {
        //设备被移除
        Logger.w("设备被移除\n" + "devId: " + devId);
    }

    @Override
    public void onStatusChanged(String devId, boolean isOnline) {
        Logger.w("设备在线状态\n" + "devId: " + devId + "  " + "isOnline: " + isOnline);
    }

    @Override
    public void onNetworkStatusChanged(String devId, boolean status) {
        //网络状态监听
        Logger.w("网络状态监听: " + status);
        if (status) {
            Logger.i("设备有网络连接\n" + "devId: " + devId);
        } else {
            Logger.w("设备无网络连接\n" + "devId: " + devId);
        }
    }

    @Override
    public void onDevInfoUpdate(String devId) {
        //设备信息变更，目前只有设备名称变化，会调用该接口
        Logger.i("设备信息变更，目前只有设备名称变化，会调用该接口\n" + "devId: " + devId);
    }
}
